<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\home\controller;

use cmf\controller\RestBaseController;
use think\Request;

class PaymentController extends RestBaseController
{

    public $payment; //  具体的支付类
    public $pay_code; //  具体的支付code

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $pay_radio = input('pay_radio');
        if(!empty($pay_radio))
        {
            $pay_radio = parse_url_param($pay_radio);
            $this->pay_code = ucfirst($pay_radio['pay_code']); // 支付 code ucfirst 讲首字母转成大写
        }
        else // 第三方 支付商返回
        {
            $this->pay_code = input('get.pay_code');
            unset($_GET['pay_code']); // 用完之后删除, 以免进入签名判断里面去 导致错误
        }
        //获取通知的数据
//        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
//        $xml = file_get_contents('php://input');
        if(empty($this->pay_code))
            exit('pay_code 不能为空');
        // 导入具体的支付类文件
//        include_once  "plugins/payment/{$this->pay_code}/{$this->pay_code}.class.php"; // D:\wamp\www\svn_tpshop\www\plugins\payment\alipay\alipayPayment.class.php
//        $code = '\\plugins\\' . '$this->pay_code' . '\\lib\\' . '$this->pay_code'; // \alipay
        $payOption = cmf_get_option('pay');
        if (empty($payOption['pay'][$this->pay_code])) $this->error("$this->pay_code 支付模块未安装，请联系管理员安装！");
        $code = $payOption['pay'][$this->pay_code]['driver'];
//        !class_exists($code) && $this->error("$this->pay_code 支付模块出错，请联系管理员！");
        $this->payment = new $code();
    }

    public function getCode()
    {
        $pay_code = 'alipay';
        $order['order_sn'] = '201811291027';
        $order['order_amount'] = 0.01;
        $config_value['pay_code'] = $pay_code;
        $config_value['body'] = '测试商品1027';
//        $className = '\\plugins\\alipay\\lib\\Alipay';
//        $class = new $className;
        $html = $this->payment->get_code($order, $config_value);
        echo $html;
//        $this->success($html);
    }

}
